#include "StdAfx.h"
#include "VNavItemTableInfo.h"
#include "../VSettings/vsettings.h"

///////////////////////////////////////////////////////////////////////////////
// VNavItemViewTableInfo //////////////////////////////////////////////////////

//
VNavItemViewTableInfo::VNavItemViewTableInfo(QWidget *parent)
	:VNavItemView(parent)
{
	m_itemFormView = VNavItemViewFormPtr(new VNavItemViewForm(this));

	QVBoxLayout *vLayout = new QVBoxLayout(this);
		vLayout->setContentsMargins(0,0,0,0);
		vLayout->addWidget(m_itemFormView.data());

	setLayout(vLayout);

/********

	m_blank = new VBlank(this);
	m_view = new VBlankView(this);

	QVBoxLayout *vLayout = new QVBoxLayout(this);
		vLayout->setContentsMargins(0,0,0,0);
		vLayout->addWidget(m_view->widget());

	setLayout(vLayout);
***********/

	//m_info = new QLabel("Info");
	//setWidget(m_view->widget());
}

//
VNavItemViewTableInfo::~VNavItemViewTableInfo()
{
}

//
void VNavItemViewTableInfo::setItem(VNavItem *item_,const QModelIndex &index)
{
	VNavItemTableInfo *item = qobject_cast<VNavItemTableInfo*>(item_);
	Q_ASSERT(item);

	QString tableName = item->tableName();
	VSqlTable sqlTable(tableName);
	
	VSqlTable sqlTableForms(VSqlRegTables::tName());

	m_itemForm.setEMode(VNavItemForm::EMode_work);
	m_itemForm.setUnicode(sqlTableForms.unicode());
	m_itemForm.setFormName(VNavItemForm::formName_default());

	m_itemFormView->setItem(&m_itemForm,QModelIndex());
	m_itemFormView->setFormParam("vUnicode",sqlTable.unicode());
	m_itemFormView->showForm();


	//VSqlFields ff = VSqlRegTables::selectFieldsAsFields(VSqlRegTables::tName());
	//VSqlFieldStringPtr f = ff.findFieldByName(VSqlRegTables::fUnicode()).objectCast<VSqlFieldString>();



/******************************
	VSqlTable sqlTable;
		sqlTable.setName(tableName);
		//sqlTable.selectTableInfo();
	
	VSqlFields fields = sqlTable.selectFields();

	foreach (const VSqlFieldPtr field,fields) {
		VComment *c = new VComment("comment");
			c->setWidth(20);
			c->setAlign(VAbstractControl::AlignLeft);
			c->setValue(field->name());
		m_blank->appendControl(c);

		VLineEdit *e = new VLineEdit(field->unicode());
			//e->setWidth(10);
			e->setAlign(VAbstractControl::AlignLeft);
			e->setLength(10);
			e->setValue("qwerty");
		m_blank->appendControl(e);

		m_blank->appendControl(new VLineBreak());
	}

	m_blank->setControlParent();	
	m_view->update(m_blank);

	connect(
		m_blank,SIGNAL( blankChanged() ),
		this,SLOT( updateBlankView() )
	);

	connect(
		m_blank,SIGNAL( blankChanged(VAbstractControl *) ),
		this,SLOT( blankChanged(VAbstractControl *) )
	);
***********************************/

/****************
	QString sFields;

	foreach (const VSqlFieldPtr field,fields) {
		sFields += field->name();

		VSqlFieldRefIdPtr fieldRefId = field.objectCast<VSqlFieldRefId>();
		if (fieldRefId) {
			sFields += "<refId:" + fieldRefId->toTableName();

			VSqlFieldPtr toField = VSqlTable::toFieldFrom(fieldRefId);
			if (toField) {
				sFields += "." + toField->name();
				sFields += " type:" + VSqlFieldType::maps.eTypeToDisplayName(toField->eType());
			}
			else {
				sFields += ".ERROR ????????????????????";
			}

			sFields += ">";
		}


		VSqlFieldRefStringPtr fieldRefString = field.objectCast<VSqlFieldRefString>();
		if (fieldRefString) {
			sFields += "<refString:" + fieldRefString->toTableName();

			VSqlFieldPtr toField = VSqlTable::toFieldFrom(fieldRefString);
			if (toField) {
				sFields += "." + toField->name();
				sFields += " type:" + VSqlFieldType::maps.eTypeToDisplayName(toField->eType());
			}
			else {
				sFields += ".ERROR ????????????????????";
			}

			sFields += ">";
		}


		VSqlFieldListIdPtr fieldListId = field.objectCast<VSqlFieldListId>();
		if (fieldListId) {
			sFields += "<listId:" + fieldListId->toTableName();

			VSqlFieldPtr toField = VSqlTable::toFieldFrom(fieldListId);
			if (toField) {
				sFields += "." + toField->name();
				sFields += " type:" + VSqlFieldType::maps.eTypeToDisplayName(toField->eType());
			}
			else {
				sFields += ".ERROR ????????????????????";
			}

			sFields += ">";
		}

		sFields += ", \r\n";
	}

	QString sInfo
		= tableName + ": \r\n" + sFields;
************************************/

	//m_info->setText(sInfo);
}
/********************
//
void VNavItemViewTableInfo::blankChanged(VAbstractControl *c)
{
	//const QString name = c->name();
	
	VAbstractControl *cc = m_blank->findControlByName("VDb/fName");
	
	if (cc)
		cc->setValue(c->value());
}
***********************/

// VNavItemTableInfo //////////////////////////////////////////////////////////

//
VNavItemTableInfo::VNavItemTableInfo()
{
}

